package com.ruoyi.common.utils;

import com.ruoyi.common.utils.http.HttpUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PackageHelper {

    protected static Log logger = LogFactory.getLog(PackageHelper.class);

    private final static String API_CALL_CHECK_KEY = "abcdefg^&%^&^2234";

    public final static String KEY = "13f439726d2d4522";

    /**
     * 获取资源上传的路径
     *
     * @param resName
     * @param version
     * @return
     */
    public static String getUploadPath(String app, String resName, int version) {
        return app + "/" + resName + "/" + version + "/" + resName;
    }

    public static Map<String, Object> syncToResupgrade(String app, String downloadPath, long size, String md5, String resName, int version, int supportVersion) {

        String url = "http://resupgrade.youzhi.net/api/resource/addVersion.do";
        Map<String, String> params = new HashMap<String, String>();
        params.put("appName", app);
        params.put("resName", resName);
        params.put("downloadUrl", downloadPath);
        params.put("size", String.valueOf(size));
        params.put("md5", md5);
        params.put("version", String.valueOf(version));
        params.put("supportVersion", String.valueOf(supportVersion));

        String sign = Md5Utils.hash(downloadPath + resName + size + md5 + API_CALL_CHECK_KEY);
        params.put("sign", sign);

        String res = HttpUtils.sendGet(url, joinParam(params));
        logger.debug(res);

        Map<String, Object> m = JsonUtil.toMap(res);

        return m;
    }

    private static String joinParam(Map<String, String> params) {

        List<String> list = new ArrayList<String>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            list.add(entry.getKey() + "=" + entry.getValue());
        }
        return jodd.util.StringUtil.join(list, "&");
    }

    public static void main(String[] args) {

    }
}
